<template>
	<view class="fittingsearch">
		<SearchBar :keyword="prodKeyword" @input="onInput" @search="onSearch" placeholder="搜索配件" />
		<view class="fittingsearch__items">
			<block v-for="(listItem, index) in accessorieList" :key="index">
				<FaultPartItem v-for="(partItem, partIndex) in listItem" :key="partItem.CustProductPartsGuid" :faultPartItem="partItem" 
				@onFaultPartItemClick="onFaultPartItemClick"/>
			</block>
		</view>
	</view>
</template>

<script>
	import faultpartsearchbehavior from './faultpartsearchbehavior';
	import SearchBar from '@/components/SearchBar';
	import FaultPartItem from '@/components/FaultPartItem';
	export default {
		mixins: [faultpartsearchbehavior],
		components: {
			SearchBar,
			FaultPartItem
		},
		data() {
			return {
				ramAccessorieList: [],
				accessorieList: [],
				prodItemObj: {},
				prodRepInfo: {},
				orderDetailId: '',
				partsKeyWord: '',
				searchType: ''
			}
		},
		onLoad(e) {
			let {
				type
			} = e;
			//debugger;
			if (type == 'fault') {
				this.searchType = 'fault';
				uni.setNavigationBarTitle({
					title: '故障件查找'
				});
			} else if (type == 'replace') {
				this.searchType = 'replace';
				uni.setNavigationBarTitle({
					title: '更换件查找'
				});
			}

		},
		onShow(e) {
			let pages = getCurrentPages();
			let prevPageData = pages[pages.length - 2].data;
			if (prevPageData.hasOwnProperty("prodItemObj") &&
				prevPageData.hasOwnProperty("prodRepInfo") &&
				prevPageData.hasOwnProperty("orderDetailId")) {
				this.prodItemObj = prevPageData.prodItemObj;
				this.prodRepInfo = prevPageData.prodRepInfo;
				this.orderDetailId = prevPageData.orderDetailId;
				//debugger;
				//console.log(prevPageData.prodItemObj, prevPageData.prodRepInfo)
				if (this.searchType == 'fault'){
					this.GetRepairSearchOldPartPageAsync();
				}
				else if (this.searchType == 'replace'){
					 this.GetRepairSearchNewPartAsync();
				}
			}
		},
		onReachBottom(e) {
			if (this.ramAccessorieList.length > 0) {
				uni.showLoading({
					mask: true,
				})
				let subAccessorieList = this.ramAccessorieList.splice(0, 10);
				this.GetPagePartImg(subAccessorieList).then((succ) => {
					this.accessorieList.push(subAccessorieList);
					uni.hideLoading()
				}, (err) => {
					uni.hideLoading()
				});
			}
		},
		methods: {
			onFaultPartItemClick(faultPartItem){
				uni.$emit("faultpartsearch",{faultPartItem:faultPartItem,type:this.searchType});
				uni.navigateBack({
				    delta: 1
				});
				// this.$emit("onFaultPartItemClick", this.faultPartItem);
			},
			
			onInput(e) {
				this.partsKeyWord = e;
				console.log(this.partsKeyWord)
			},

			onSearch(e) {
				console.log(e);
				this.partsKeyWord = e;
				this.ramAccessorieList = [];
				this.accessorieList = [];
				if (this.searchType == 'fault')
					this.GetRepairSearchOldPartPageAsync();
				else if (this.searchType == 'replace')
				    this.GetRepairSearchNewPartAsync();
			},

			//故障件查找
			async GetRepairSearchOldPartPageAsync() {
				uni.showLoading({
					mask: true,
				})
				this.ramAccessorieList = await this.RepairSearchOldPartAsync();
				let subAccessorieList = this.ramAccessorieList.splice(0, 10);
				subAccessorieList = this.uniqueArray(subAccessorieList, "PartsGuid")
				console.log(subAccessorieList);
				await this.GetPagePartImg(subAccessorieList);
				this.accessorieList.push(subAccessorieList);
				console.log(this.accessorieList);
				uni.hideLoading()
			},

			//更换件查找
			async GetRepairSearchNewPartAsync() {
				uni.showLoading({
					mask: true,
				})
				this.ramAccessorieList = await this.RepairSearchNewPartAsync();
				let subAccessorieList = this.ramAccessorieList.splice(0, 10);
				subAccessorieList = this.uniqueArray(subAccessorieList, "PartsGuid")
				console.log(subAccessorieList);
				await this.GetPagePartImg(subAccessorieList);
				this.accessorieList.push(subAccessorieList);
				console.log(this.accessorieList);
				uni.hideLoading()
			},

			async GetPagePartImg(subAccessorieList) {
				let imageArray = new Array();
				for (let accessorIndex in subAccessorieList) {
					let ImgId = subAccessorieList[accessorIndex].ImgId;
					if (ImgId) {
						imageArray.push(ImgId)
					}
				}
				let imageids = imageArray.join(",");
				let imgresult = await this.GetPartImg(imageids);

				for (let imgindex in imgresult) {
					let accessList = subAccessorieList.filter(accessItem => {
						return imgresult[imgindex].ImgId === accessItem.ImgId
					});
					if (accessList.length > 0) {
						for (let accessIndex in accessList)
							accessList[accessIndex].base64Img = `data:image/jpeg;base64,${imgresult[imgindex].ImgBaseStr}`;
					}
				}
			},
			//去掉重复数据
			uniqueArray(arrobj, attrName) {
				let res = [];
				for (let i in arrobj) {
					res.findIndex((item, index, arr) => {
						return item[attrName] === arrobj[i][attrName]
					}) !== -1 ? '' : res.push(arrobj[i]);
				}
				return res;
			}

		}
	}
</script>

<style lang="scss">
	@import './faultpartsearch.scss'
</style>
